
//
// 120317 - AH - (Huset *) Created.
// 120727 - AH - (Midtun) Changed name from SoekratResponse. 
//

package org.soekrat.server.servlets;

import java.io.Serializable;

/**
 * <p>
 *   Adds status to 
 *   {@link Serializable} data to be sent to client as response.
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC)
 */
public final class SoekratResponse implements Serializable 
{
  private static final long serialVersionUID=-6017803148953602729L;

  private String status="ok";
  
  private Object response=null;
  
  /**
   * <p>
   *   Constructor.
   * </p>
   */
  public SoekratResponse(){}
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param response Data to be serialized with {@code this}.
   */
  public SoekratResponse(Object response){ response(response); }
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param status Status message. Anything but "ok" (the default) is an error
   *               message.
   */
  public SoekratResponse(String status){ status(status); }
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param status Status message. Anything but "ok" (the default) is an error
   *               message.
   */
  public SoekratResponse(String status, Object response)
  {
    status(status);
    response(response);
  }
  
  /**
   * <p>
   *   Sets status message.
   * </p>
   * @param status Status message. Anything but "ok" (the default) is an error
   *               message.
   * @return {@code this}.
   */
  public SoekratResponse status(String status)
  {
    if (status==null)
    {
      throw new NullPointerException("status");
    }
    
    status=status.trim();
    if (status.length()==0)
    {
      throw new IllegalArgumentException("status can not be the empty string");
    }
    
    this.status=status;
    
    return this;
  }
  
  /**
   * <p>
   *   Gets status message.
   * </p>
   * @return Status message.
   */
  public String status(){ return status; }
  
  /**
   * <p>
   *   Sets data to be serialized with {@code this}.
   * </p>
   * @param response Data.
   * @return {@code this}.
   */
  public SoekratResponse response(Object response)
  {
    if (response==null)
    {
      throw new NullPointerException("response");
    }
    
    this.response=response;
    
    return this;
  }
  
  /**
   * <p>
   *   Gets data to be serialized with {@code this}.
   * </p>
   * @return Data.
   */
  public Object response(){ return response; }
  
}
